### REPLICATION FILE
### Ian Budge & Jonathan Homola
### "How Far Have European Political Parties Followed the Americans 
### to the Right in the Later Post-War Period? -- A Textual Analysis"
### Cambio 2: 71-86
### http://dx.doi.org/10.13128/cambio-19435

## clear environment, set seed, install/load packages
rm(list=ls())
set.seed(12435); options(stringsAsFactors=F)
#install.packages("foreign")
library("foreign")

## set working directory
#setwd(" ... ")

## read in the main dataset 
cambiodata <- read.dta("BudgeHomola_PartyMovements_Data.dta")
cambiotable <- subset(cambiodata, useintable==1)

## create and fill results matrix "cambioresults"
cambioresults <- as.data.frame(matrix(NA, nrow=78, ncol=4))
colnames(cambioresults) <- c("Country", "All parties", "Larger parties", "MV position")
j <- 1
for (i in unique(cambiotable$country)){
  cambioresults[j,1] <- i
  cambioresults[j+1,1] <- "(se)"
  countrydata <- subset(cambiotable, country==i)
  cambioresults[j,2] <- round(summary(lm(rile~time, countrydata))$coefficients[2,1], 3)
  cambioresults[j+1,2] <- round(summary(lm(rile~time, countrydata))$coefficients[2,2], 3)
  cambioresults[j,3] <- round(summary(lm(rile~time, subset(countrydata, pervote>5)))$coefficients[2,1], 3)
  cambioresults[j+1,3] <- round(summary(lm(rile~time, subset(countrydata, pervote>5)))$coefficients[2,2], 3)
  countrydata <- countrydata[with(countrydata, do.call(order, list(time))), ]
  countrydata <- do.call(rbind, by(countrydata, list(countrydata$time),
                                   FUN=function(x) head(x, 1)))
  cambioresults[j,4] <- round(summary(lm(median~time, countrydata))$coefficients[2,1], 3)
  cambioresults[j+1,4] <- round(summary(lm(median~time, countrydata))$coefficients[2,2], 3)
  j <- j+2
}

europe <- subset(cambiodata, europe==1)
weurope <- subset(cambiodata, weurope==1)

cambioresults[j,1] <- "Europe"
cambioresults[j+1,1] <- "(se)"
countrydata <- europe
cambioresults[j,2] <- round(summary(lm(rile~time, countrydata))$coefficients[2,1], 3)
cambioresults[j+1,2] <- round(summary(lm(rile~time, countrydata))$coefficients[2,2], 3)
cambioresults[j,3] <- round(summary(lm(rile~time, subset(countrydata, pervote>5)))$coefficients[2,1], 3)
cambioresults[j+1,3] <- round(summary(lm(rile~time, subset(countrydata, pervote>5)))$coefficients[2,2], 3)
countrydata <- countrydata[with(countrydata, do.call(order, list(country, time))), ]
countrydata <- do.call(rbind, by(countrydata, list(countrydata$country, countrydata$time),
                                 FUN=function(x) head(x, 1)))
cambioresults[j,4] <- round(summary(lm(median~time, countrydata))$coefficients[2,1], 3)
cambioresults[j+1,4] <- round(summary(lm(median~time, countrydata))$coefficients[2,2], 3)
j <- j+2

cambioresults[j,1] <- "Western Europe"
cambioresults[j+1,1] <- "(se)"
countrydata <- weurope
cambioresults[j,2] <- round(summary(lm(rile~time, countrydata))$coefficients[2,1], 3)
cambioresults[j+1,2] <- round(summary(lm(rile~time, countrydata))$coefficients[2,2], 3)
cambioresults[j,3] <- round(summary(lm(rile~time, subset(countrydata, pervote>5)))$coefficients[2,1], 3)
cambioresults[j+1,3] <- round(summary(lm(rile~time, subset(countrydata, pervote>5)))$coefficients[2,2], 3)
countrydata <- countrydata[with(countrydata, do.call(order, list(country, time))), ]
countrydata <- do.call(rbind, by(countrydata, list(countrydata$country, countrydata$time),
                                 FUN=function(x) head(x, 1)))
cambioresults[j,4] <- round(summary(lm(median~time, countrydata))$coefficients[2,1], 3)
cambioresults[j+1,4] <- round(summary(lm(median~time, countrydata))$coefficients[2,2], 3)
j <- j+2



#### Table 3
cambioresults